﻿
using System.Collections.Generic;
using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

public class DaoBehaviour4 : SubsystemBlockBehavior, IUpdateable
{
    public SubsystemTerrain m_subsystemTerrain;

    public SubsystemTime m_subsystemTime;

    public SubsystemProjectiles m_subsystemProjectiles;

    public SubsystemParticles m_subsystemParticles;

    public SubsystemAudio m_subsystemAudio;

    public SubsystemNoise m_subsystemNoise;

    public SubsystemPlayers m_subsystemPlayers;

    public SubsystemGameInfo m_subsystemGameInfo;

    public ComponentLevel m_componentLevel;

    public Game.Random m_random = new Game.Random();

    public Dictionary<ComponentMiner, double> m_aimStartTimes = new Dictionary<ComponentMiner, double>();

    public override int[] HandledBlocks => new int[0];

    public ComponentPlayer componentPlayer;

    public ComponentMiner componentMiner;
    public float GunChangeActionPross = 0f;

    public IInventory m_inventory;

    public int m_slotIndex;

    public UpdateOrder UpdateOrder
    {
        get
        {
            return 0;
        }
    }

    public void Update(float dt)
    {
    }

    public override bool OnAim(Ray3 aim, ComponentMiner componentMiner, AimState state)
    {
        IInventory inventory = componentMiner.Inventory;
        float x = componentMiner.ComponentPlayer.ComponentBody.StanceBoxSize.Y;
        Vector3 playerPosition = componentMiner.ComponentPlayer.ComponentBody.Position;
        if (inventory != null)
        {
            int num3 = 10;
            int activeSlotIndex = inventory.ActiveSlotIndex;
            Vector3 vector = Vector3.Zero;
            Vector3 vector2 = componentMiner.ComponentCreature.ComponentCreatureModel.EyePosition + componentMiner.ComponentCreature.ComponentBody.Matrix.Right * 0.3f - componentMiner.ComponentCreature.ComponentBody.Matrix.Up * 0.2f;
            Vector3 vector3 = Vector3.Normalize(vector2 + aim.Direction * 10f - vector2);
            Vector3 vector4 = Vector3.Normalize(Vector3.Cross(vector3, Vector3.UnitY));
            Vector3 vector5 = Vector3.Normalize(Vector3.Cross(vector3, vector4));
            Vector3 vector6 = m_random.Float(0f - vector.X, vector.X) * vector4 + m_random.Float(0f - vector.Y, vector.Y) * vector5 + m_random.Float(0f - vector.Z, vector.Z) * vector3;
            Vector3 position = vector2;
            Vector3 velocity = 120f * (vector3 + vector6);
            Vector3 velocity2 = 120f * (vector3 + vector6 + m_random.Vector3(0.05f));
            Vector3 velocity3 = 120f * (vector3 + vector6 + m_random.Vector3(0.1f));
            Vector3 velocity4 = 120f * (vector3 + vector6 + m_random.Vector3(0.15f));
            Vector3 velocity5 = 120f * (vector3 + vector6 + m_random.Vector3(0.2f));
            Vector3 velocity6 = 120f * (vector3 + vector6 + m_random.Vector3(0.25f));
            Vector3 velocity7 = 120f * (vector3 + vector6 + m_random.Vector3(0.3f));
            Vector3 velocity8 = 120f * (vector3 + vector6 + m_random.Vector3(0.35f));
            Vector3 velocity9 = 120f * (vector3 + vector6 + m_random.Vector3(0.45f));
            Vector3 velocity10 = 120f * (vector3 + vector6 + m_random.Vector3(0.4f));
            Vector3 vimpulse = -0.2f * vector3;
            float Velocity2 = (float)componentMiner.ComponentPlayer.ComponentLocomotion.WalkSpeed;
            int slotValue = inventory.GetSlotValue(activeSlotIndex);
            int slotCount = inventory.GetSlotCount(activeSlotIndex);
            int num = Terrain.ExtractContents(slotValue);
            int data = Terrain.ExtractData(slotValue);
            int num2 = slotValue;
            if (!m_aimStartTimes.TryGetValue(componentMiner, out double value))
            {
                value = m_subsystemTime.GameTime;
                m_aimStartTimes[componentMiner] = value;
            }
            float num4 = (float)(m_subsystemTime.GameTime - value);
            float num5 = (float)MathUtils.Remainder(m_subsystemTime.GameTime, 1000.0);
            Vector3 v = ((componentMiner.ComponentCreature.ComponentBody.IsSneaking ? 0.01f : 0.03f) + 0.2f * MathUtils.Saturate((num4 - 2.5f) / 6f)) * new Vector3
            {
                X = SimplexNoise.OctavedNoise(num5, 2f, 3, 2f, 0.5f),
                Y = SimplexNoise.OctavedNoise(num5 + 100f, 2f, 3, 2f, 0.5f),
                Z = SimplexNoise.OctavedNoise(num5 + 200f, 2f, 3, 2f, 0.5f)
            };
            aim.Direction = Vector3.Normalize(aim.Direction + v);
            switch (state)
            {
                case AimState.InProgress:
                    {
                        m_subsystemParticles.AddParticleSystem(new PlayerParticle2(m_subsystemTerrain, playerPosition, x));
                        componentMiner.ComponentCreature.ComponentCreatureModel.AimHandAngleOrder = 1.4f;
                        componentMiner.ComponentCreature.ComponentCreatureModel.InHandItemOffsetOrder = new Vector3(-0.08f, -0.08f, 0.07f);
                        componentMiner.ComponentCreature.ComponentCreatureModel.InHandItemRotationOrder = new Vector3(-0.7f, 0f, 0f);
                        ComponentFirstPersonModel componentFirstPersonModel = componentMiner.Entity.FindComponent<ComponentFirstPersonModel>();
                        if (num4 <= 2f)
                        {
                            componentMiner.ComponentPlayer?.ComponentAimingSights.ShowAimingSights(aim.Position, aim.Direction);
                            componentFirstPersonModel.ItemOffsetOrder = new Vector3(-0.21f, 0.15f, 0.08f);
                            componentFirstPersonModel.ItemRotationOrder = new Vector3(-0.7f, 0f, 0f);
                            componentFirstPersonModel.ItemRotationOrder = new Vector3(1f, -0.5f, 0.3f);
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("蓄力中...", Color.Red, blinking: true, playNotificationSound: false);
                        }
                        if (num4 >= 10f)
                        {
                            componentMiner.ComponentCreature.ComponentCreatureSounds.PlayMoanSound();
                            return true;
                        }
                        if (num4 > 2f && !MusketBlock.GetHammerState(Terrain.ExtractData(num2)))
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("蓄力完成...", Color.Red, blinking: true, playNotificationSound: false);
                            componentFirstPersonModel.ItemRotationOrder = new Vector3(1f, -0.5f, 0.3f);
                        }
                        if (num4 <= 2f)
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("蓄力未完成...", Color.White, blinking: true, playNotificationSound: false);
                        }
                        if (num4 > 2f)
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("松手瞬移", Color.Red, blinking: true, playNotificationSound: false);
                        }
                        break;
                    }
                case AimState.Completed:
                    {
                        if (num4 <= 2f)
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("蓄力未完成...", Color.White, blinking: true, playNotificationSound: false);
                        }
                        if (num4 > 2f)
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("瞬移，但消耗了你一点内法", Color.Red, blinking: true, playNotificationSound: false);
                            componentMiner.ComponentPlayer.ComponentBody.Position += 2 * Velocity2 * componentMiner.ComponentPlayer.ComponentBody.Matrix.Forward;//向前瞬移一段距离，距离与速度有关
                            componentMiner.ComponentPlayer.ComponentVitalStats.Food -= 0.1f;
                            AudioManager.PlaySound("Audio/sj", 1f, 0f, 0f);
                            m_subsystemNoise.MakeNoise(position, 1f, 40f);
                            componentMiner.ComponentCreature.ComponentBody.ApplyImpulse(vimpulse);
                            componentMiner.DamageActiveTool(num3);
                        }
                        break;
                    }
                case AimState.Cancelled:
                    {
                        m_aimStartTimes.Remove(componentMiner);
                        break;
                    }
            }
        }

        return false;
    }
    public override bool OnHitAsProjectile(CellFace? cellFace, ComponentBody componentBody, WorldItem worldItem)
    {
        if (worldItem.Velocity.Length() > 10f && m_random.Float(0f, 1f) < 0.5f)
        {
            return true;
        }
        return false;
    }
    public override void Load(ValuesDictionary valuesDictionary)
    {
        m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
        m_subsystemPlayers = Project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
        m_subsystemGameInfo = Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
        m_subsystemTime = Project.FindSubsystem<SubsystemTime>(throwOnError: true);
        m_subsystemProjectiles = Project.FindSubsystem<SubsystemProjectiles>(throwOnError: true);
        m_subsystemParticles = Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
        m_subsystemAudio = Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
        m_subsystemNoise = Project.FindSubsystem<SubsystemNoise>(throwOnError: true);
        base.Load(valuesDictionary);
    }
}
